package com.campus.dao.impl;

import com.campus.dao.CourseDao;
import com.campus.model.Course;
import com.campus.model.LearningUnit;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @desc: 课程数据访问实现类
 * @author: hjm
 * @date: 2024/11/22
 */
@Repository
public class CourseDaoImpl implements CourseDao {

    private final JdbcTemplate jdbcTemplate;

    public CourseDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 创建课程
     *
     * @param course 课程对象
     * @return 受影响的行数
     */
    @Override
    public int createCourse(Course course) {
        try {
            String sql = "INSERT INTO t_course (course_name, created_time, status, activity_id, teacher_id, class_id) VALUES (?, ?, ?, ?, ?, ?)";
            return jdbcTemplate.update(sql,
                    course.getCourseName(),
                    course.getCreatedTime(),
                    course.getStatus(),
                    course.getActivityId(),
                    course.getTeacherId(),
                    course.getClassId());
        } catch (Exception e) {
            System.err.println("创建课程时发生错误: " + e.getMessage());
            return 0;
        }
    }

    /**
     * 创建学习单元
     *
     * @param learningUnit 学习单元对象
     * @return 受影响的行数
     */
    @Override
    public int createLearnUnit(LearningUnit learningUnit) {
        String sql = "INSERT INTO t_course_learn_unit (unit_title, doc_path, video_path, unit_introduce, father_id, is_chpt, course_id, learning_score) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                learningUnit.getUnitTitle(),
                learningUnit.getDocPath(),
                learningUnit.getVideoPath(),
                learningUnit.getUnitIntroduce(),
                learningUnit.getFatherId(),
                learningUnit.getIsChpt(),
                learningUnit.getCourseId(),
                learningUnit.getLearningScore());
    }

    /**
     * 根据ID获取学习单元信息
     *
     * @param unitId 学习单元ID
     * @return 学习单元对象
     */
    @Override
    public LearningUnit getLearnUnitById(int unitId) {
        String sql = "SELECT * FROM t_course_learn_unit WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{unitId}, (rs, rowNum) -> {
            LearningUnit learningUnit = new LearningUnit();
            learningUnit.setId((long) rs.getInt("id"));
            learningUnit.setUnitTitle(rs.getString("unit_title"));
            learningUnit.setDocPath(rs.getString("doc_path"));
            learningUnit.setVideoPath(rs.getString("video_path"));
            learningUnit.setUnitIntroduce(rs.getString("unit_introduce"));
            learningUnit.setFatherId((long) rs.getInt("father_id"));
            learningUnit.setIsChpt(rs.getString("is_chpt"));
            learningUnit.setCourseId((long) rs.getInt("course_id"));
            learningUnit.setLearningScore(rs.getInt("learning_score"));
            return learningUnit;
        });
    }

    /**
     * 根据课程ID获取学习单元列表
     *
     * @param courseId 课程ID
     * @return 学习单元列表
     */
    @Override
    public List<LearningUnit> getLearnUnitsByCourseId(int courseId) {
        String sql = "SELECT * FROM t_course_learn_unit WHERE course_id = ?";
        return jdbcTemplate.query(sql, new Object[]{courseId}, (rs, rowNum) -> {
            LearningUnit learningUnit = new LearningUnit();
            learningUnit.setId((long) rs.getInt("id"));
            learningUnit.setUnitTitle(rs.getString("unit_title"));
            learningUnit.setDocPath(rs.getString("doc_path"));
            learningUnit.setVideoPath(rs.getString("video_path"));
            learningUnit.setUnitIntroduce(rs.getString("unit_introduce"));
            learningUnit.setFatherId((long) rs.getInt("father_id"));
            learningUnit.setIsChpt(rs.getString("is_chpt"));
            learningUnit.setCourseId((long) rs.getInt("course_id"));
            learningUnit.setLearningScore(rs.getInt("learning_score"));
            return learningUnit;
        });
    }
}